package com.nuc.kl.module.usermanager.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuc.kl.base.DaoSupportImpl;
import com.nuc.kl.module.basedata.entity.Navigation;
import com.nuc.kl.module.usermanager.entity.Privilege;
import com.nuc.kl.module.usermanager.entity.Role;
import com.nuc.kl.module.usermanager.entity.TreeDTO;
import com.nuc.kl.module.usermanager.entity.User;
import com.nuc.kl.module.usermanager.service.PrivilegeService;

@Service
@Transactional
public class PrivilegeServiceImpl extends DaoSupportImpl<Privilege> implements PrivilegeService{
	public List<Privilege> findTopList() {
		return getSession().createQuery(//
				"FROM Privilege p WHERE p.parent IS NULL")//
				.list();
	}

	public Collection<String> getAllPrivilegeUrls() {
		return getSession().createQuery(//
				"SELECT DISTINCT p.url FROM Privilege p WHERE p.url IS NOT NULL")//
				.list();
	}
	
	public Collection<String> getAllPrivilegeNames() {
		return getSession().createQuery(//
				"SELECT  p.name FROM Privilege p")//
				.list();
	}
	

	/**
	 * 根据父id获取子节点们 
	 */
	public List<TreeDTO> getChildrenByParentId(String id)  {
		
		String hql = "";
		List<Privilege> privilegeList = new ArrayList<Privilege>();
		if("".equals(id) || id == null){
			hql = "from Privilege p where p.parent is null";
			privilegeList = getSession().createQuery(hql).list();
		} else {
			hql = " from Privilege p  where p.parent.id =?"  ;
			privilegeList = getSession().createQuery(hql)//
			.setParameter(0, Long.parseLong(id)).list();
		}
		
		List<TreeDTO> tlist = new ArrayList<TreeDTO>();
		for (Iterator iterator = privilegeList.iterator(); iterator.hasNext();) {
			Privilege privilege = (Privilege) iterator.next();
			TreeDTO tree = new TreeDTO();
			tree.setId(privilege.getId());
			tree.setText(privilege.getName());
			tree.setChecked(privilege.getChecked());
			tree.setIconCls(privilege.getIcon());
			if(privilege.getParent()==null){
				tree.setParent_id(null);
			}else{
				tree.setParent_id(privilege.getParent().getId());
			}
		
			
			if(getChildren(privilege.getId()).size() > 0){
				tree.setState("closed");
			} else {
				tree.setState("open");
			}
			Map<String, Object>  map = new HashMap<String, Object>();
			map.put("url", privilege.getUrl());
			tree.setAttributes(map);
			tlist.add(tree);
		}
		
		

		
		
		return tlist;
	}


	
	
	/**
	 * 根据pid 获取孩子 
	 * @param pid
	 * @return
	 * @throws Exception
	 */
	public List<Privilege> getChildren(Long pid){
		String hql = " from Privilege p where p.parent.id =?";
		
		List<Privilege> children = new ArrayList<Privilege>();
		children = getSession().createQuery(hql).setParameter(0,pid).list();
		return children;
	}
	
public List<Privilege> findList(Long parentId) {
		
		if(parentId == null){
			return getSession().createQuery(//
			"FROM Privilege n WHERE n.parent.id IS null")//
			.list();
		}else{
			return listChildren(parentId);
		}
		
	}

	public List<Privilege> listChildren(Long parentId) {
		return getSession().createQuery(//
		"FROM Privilege n WHERE n.parent.id = ?")//
		.setParameter(0, parentId)//
		.list();
	}

	
	public List<String> getEntityPrivilege(User user) {
		List<String> privilegeList = new ArrayList<String>();
		
		if("admin".equals(user.getName())){
			privilegeList = (List<String>) getAllPrivilegeNames();
		}else{
		Set<Role> roleList = user.getRoles();	
		for (Role role : roleList) {
			Set<Privilege> privileges = role.getPrivileges();
			for (Privilege privilege : privileges) {
				privilegeList.add(privilege.getName());
			}
		}
		}
		return privilegeList;
	}

	public String getUrlByName(String name) {
		
		return (String) getSession().createQuery("select p.url form Privilege p where name=?")//
		.setParameter(0,name).uniqueResult();
	}
	
}

